import { Request } from "./request";
import {loginData, recordSearch, onlineList, user, bindSearch, password, userList} from '@/interface/type';
import { it } from "element-plus/lib/locale";
import { flushCookie } from '../utils/cookie'
import router from '../router/index'
import exp from "constants";

/**
 * 登录
 * @param info 用户名，密码
 * @returns 
 */
export function login(info: loginData) {
    return Request.axiosInstance({
        url: 'auth/login',
        method: 'post',
        data:  info
    })
}
/**
 * 登出
 */
export function accountLogOut() {
    flushCookie('auth')
    return Request.axiosInstance({
        url: 'auth/logout',
        method: 'post',
    })
  
}
/**
 * 修改密码
 */
export function editPass(item: password) {
    return Request.axiosInstance({
        url: '/auth/password',
        method: 'post',
        data: item
    })
}
/**
 * 获取当前用户信息
 */
export function getInfo(param?:any){
    return Request.axiosInstance({
        url: 'auth/me',
        method: 'get',
        data: param
    })
}

/**
 * 查询会话
 */
export function getRecord(item?: recordSearch) {

    let curUrl = '/conversation/search'
    let num = 1;
    if(item) {
        curUrl += '?';
        if(item.page) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'page=' + item.page;
            num++;
        }
        if(item.size) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'size=' + item.size;
            num++;
        }
        if(item.cname) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'counsellorName=' + item.cname;
            num++;
        }
        
        if(item.vname) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'visitorName=' + item.vname;
            num++;
        }
        

        if(item.startTime) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'startTime=' + item.startTime;
            num++;
        }
        if(item.endTime) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'endTime=' + item.endTime;
            num++;
        }
    }
    return Request.axiosInstance({
        url: curUrl,
        method: 'get',
    })
}

/**
 * 查询排班
 */
export function getArrange(item?: number) {
    let url = '/arrange/get';
    let num = 1;
    if (item) {
        url += '?'
        url = url + 'arrangeTime=' + item;
        num ++;
    }

    return Request.axiosInstance({
        url: url,
        method: 'get',
    })
}

/**
 * 获取当天咨询时长
 */
export function getDuration() {
    return Request.axiosInstance({
        url: '/conversation/today_stat',
        method: 'GET'
    })
}
/**
 * 获取当前用户列表
 * @param item 
 * @returns 
 */
export function getCounsellorList(item: userList) {
    let curUrl = '/user/list';
    let num = 1;
    if (item) {
        curUrl += '?'
        if(item.page) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'page=' + item.page;
            num++;
        }
        if(item.size) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'size=' + item.size;
            num++;
        }
        if(item.role) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'role=' + item.role;
            num++;
        }
        if(item.name) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'name=' + item.name;
            num++;
        }
        
    }

    return Request.axiosInstance({
        url: curUrl,
        method: 'get',
    })
}
/**
 * 修改个人信息
 */
export function editInfo(item: user) {
    return Request.axiosInstance({
        url: 'auth/me',
        method: 'post',
        data: item
    })

}
/**
 * 查看具体记录
 */
export function getDetail(item?: string) {
    let curUrl = '/conversation/detail';
    let num = 1;
    if(item) {
        curUrl += '?';
        curUrl = curUrl + 'conversationID=' + item; 
        
    }
    return Request.axiosInstance({
        url: curUrl,
        method: 'get',
    })
}


/**
 * 导出单条记录
 */
export function getExportSingle(item?: string) {
    let curUrl = '/api/conversation/export';
    let num = 1;
    if(item) {
        curUrl += '?';
        curUrl = curUrl + 'conversationID=' + item; 
        num ++;
        
    }
    window.location.href = curUrl;
}

/**
 * 查询绑定关系
 */
export function searchBind(item: bindSearch) {
    let curUrl = '/user/bind';
    let num = 1;
    if(item) {
        curUrl += '?';
        if(item.counsellorID) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'counsellorID=' + item.counsellorID;
            num++;
        }
        if(item.bindingID) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'bindingID=' + item.bindingID;
            num++;
        }
        
        if(item.supervisorID) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'supervisorID=' + item.supervisorID;
            num++;
        }
        
        if(item.name) {
            if (num > 1) {
                curUrl += '&'
            }
            curUrl = curUrl + 'name=' + item.name;
            num++;
        }
        
        
    }
    return Request.axiosInstance({
        url: curUrl,
        method: 'get',
    })
}
/**
 * 添加绑定
 */
export function addBind(item: bindSearch) {
    return Request.axiosInstance({
        url: '/user/bind',
        method: 'PUT',
        data: item
    })
}
/**
 * 图片上传
 */
export function upload(item: FormData) {
    return Request.axiosInstance({
        url: '/image_upload',
        method: 'put',
        data: item
    })
}
/**
 * 头像上传
 * @param item 
 * @returns 
 */
export function uploadAvatar(item: any) {
    return Request.axiosInstance({
        url: '/auth/avatar_upload',
        method: 'post',
        data: item
    })
}
/**
 * 删除绑定
 */
export function deleteBind(item: any){
    return Request.axiosInstance({
        url: '/user/bind',
        method: 'delete',
        data: item
    })
}


/**
 * 导出全部记录
 */




/*
    WARNING: DO NOT USE THESE FUNCTION
    WILL BE DELETED LATER
*/

export function addBind_IM(user1 :any,user2:any){
    let base_url = 'https://console.tim.qq.com/v4/sns/friend_add?sdkappid=1400637394&identifier=administrator&usersig=eAEtzcsOgjAUBNB-uVsMFKjlkbggrjAuNAILd42teEOApi34iv8uAZZzMpP5QnG8uKPUkELgEtjMGYXsLN5xZi5a7NBYzW2v14IRDVcKBaQ*JYSFUZjQZSpfCrWElBEaE7KYxXYSn9EkDmgSrWqwnk4dpyqag-H2mLN3X7S8H8rqkz2VR3P-ygazHZvTo6yz820Hvz8D*DTC&random=341234&contenttype=json'
    let json = 
    {
        'From_Account': user1,
        'AddFriendItem': [
            {
                'To_Account': user2,
                'AddSource': 'AddSource_Type_Admin'
            }], 
        'AddType': 'Add_Type_Both', 
        'ForceAddFlags': 1
    }
    return Request.axiosInstance({
        url: base_url,
        method: 'post',
        data: json
    })
}